home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / configglue / glue.py < prev    next >
Encoding:
Python Source  |  2009-06-15  |  2.6 KB  |  79 lines

  1. # This file is part of configglue, by John R. Lenton <john.lenton@canonical.com>
  2. # (C) 2009 by Canonical Ltd.
  3. # Released under the BSD License (see the file LICENSE)
  4. # For bug reports, support, and new releases: http://launchpad.net/configglue
  5.  
  6. """configglue lives here
  7. """
  8. from __future__ import absolute_import
  9.  
  10. from .typed import TypedConfigParser
  11. from optparse import OptionParser
  12.  
  13. __all__ = ('configglue',)
  14.  
  15. def normoptname(parser, section, option):
  16.     """Helper that handles the special __main__ section for option names"""
  17.     if section == '__main__':
  18.         return parser.optionxform(option)
  19.     else:
  20.         return parser.optionxform(section +'-'+ option)
  21.     
  22.  
  23. def configglue(fileobj, *filenames, **kwargs):
  24.     """Populate an OptionParser with options and defaults taken from a
  25.     series of files.
  26.  
  27.     @param fileobj: An INI file, as a file-like object.
  28.     @param filenames: An optional series of filenames to merge.
  29.     @param kwargs: options passed on to the OptionParser constructor except for:
  30.     @param args: parse these args (defaults to sys.argv[1:])
  31.     @param extra_parsers: list of (name, parser) parser tuples to add to the 
  32.                           Configparser
  33.     """
  34.     cp = TypedConfigParser()
  35.  
  36.     if 'extra_parsers' in kwargs:
  37.         for extra_parser in kwargs.pop('extra_parsers'):
  38.             cp.add_parser(*extra_parser)
  39.  
  40.     cp.readfp(fileobj)
  41.     cp.read(filenames)
  42.     cp.parse_all()
  43.  
  44.     args = kwargs.pop('args', None)
  45.  
  46.     op = OptionParser(**kwargs)
  47.         
  48.     for section in cp.sections():
  49.         if section == '__main__':
  50.             og = op
  51.             tpl = '--%(option)s'
  52.         else:
  53.             og = op.add_option_group(section)
  54.             tpl = '--%(section)s-%(option)s'
  55.         for optname in cp.options(section):
  56.             option = cp.get(section, optname)
  57.             if 'help' in option.attrs:
  58.                 option.attrs['help'] %= option.attrs
  59.             if option.is_empty:
  60.                 default = None
  61.             else:
  62.                 default = option.value
  63.             og.add_option(tpl % {'section': section.lower(),
  64.                                  'option': optname.lower()},
  65.                           **dict(option.attrs, default=default))
  66.                 
  67.     options, args = op.parse_args(args)
  68.  
  69.     for section in cp.sections():
  70.         for optname, optval in cp.items(section):
  71.             optname = normoptname(cp, section, optname)
  72.             value = getattr(options, optname)
  73.             if optval.value != value:
  74.                 # the value has been overridden by an argument;
  75.                 # re-parse it.
  76.                 setattr(options, optname, optval.parser(value))
  77.  
  78.     return op, options, args
  79.